//
//  JGBaseTableViewCell.h
//  JGExpandTableDemo2
//
//  Created by mac on 15/9/21.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGBaseTableViewCell.h"
#import "JGBadgeView.h"

@interface JGBaseTableViewCell()

@property (nonatomic, strong) JGBadgeView *badgeView;

@end

@implementation JGBaseTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        UIView *backgroundView = [[UIView alloc] initWithFrame:self.frame];
        backgroundView.backgroundColor = [UIColor whiteColor];
        self.backgroundView = backgroundView;
        
        self.badgeView = [[JGBadgeView alloc] init];
        
        [self.contentView addSubview:self.badgeView];
        
        self.badgeView.hidden = YES;
        
    }
    return self;
}

- (void)setBadgeNum:(NSInteger)num {
    self.badgeView.badgeString = (num == -1 ? @"" : (num > 99 ? @"99+" : [NSString stringWithFormat:@"%ld", (long)num]));
    if (num == 0) {
        self.badgeView.hidden = YES;
    } else {
        self.badgeView.hidden = NO;
        self.badgeView.origin = CGPointMake(self.imageView.right - self.badgeView.width*2/3, self.imageView.top - self.badgeView.height/3);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.badgeView.left = self.imageView.right - self.badgeView.width*2/3;
    self.badgeView.top = self.imageView.top - self.badgeView.height/4;
    [self.contentView bringSubviewToFront:self.badgeView];
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
